package jjn.round2;

/**
 * @author Jiang Jining
 * @since 2023-05-09 23:17
 */
public class LeetCode2437_NumberOfValidClockTimes {
    public int countTime(String time) {
        String[] parts = time.split(":");
        if (parts.length < 2) {
            throw new IllegalArgumentException("time格式有误");
        }
        String first = parts[0];
        String second = parts[1];
        int timeOfFirst = getTimeOfFirst(first);
        int timeOfSecond = getTimeOfSecond(second);
        return timeOfFirst * timeOfSecond;
    }
    
    private int getTimeOfSecond(String second) {
        if (second == null || second.length() != 2) {
            throw new IllegalArgumentException("time格式有误");
        }
        if ("??".equals(second)) {
            return 60;
        }
        if (second.startsWith("?")) {
            return 6;
        }
        if (second.endsWith("?")) {
            return 10;
        }
        return 1;
    }
    
    private int getTimeOfFirst(String first) {
        if (first == null || first.length() != 2) {
            throw new IllegalArgumentException("time格式有误");
        }
        if ("??".equals(first)) {
            return 24;
        }
        if (first.startsWith("?")) {
            int i = Integer.parseInt(first.substring(1));
            if (i < 4) {
                return 3;
            } else {
                return 2;
            }
        }
        if (first.endsWith("?")) {
            int i = Integer.parseInt(first.substring(0, 1));
            if (i == 2) {
                return 4;
            } else {
                return 10;
            }
        }
        return 1;
    }
}
